<template>
  <el-dialog v-model="modelValue" title="发布作业" width="500px">
    <el-form ref="assignmentFormRef" :model="data" label-width="80px">
      <el-form-item
        label="作业描述"
        prop="des"
        :rules="{ required: true, message: '请输入作业描述', trigger: 'blur' }"
      >
        <el-input v-model="data.des" placeholder="请输入作业标题"></el-input>
      </el-form-item>
      <el-form-item
        label="所属课程"
        prop="courseId"
        :rules="{ required: true, message: '选择作业描述', trigger: 'change' }"
      >
        <el-select v-model="data.courseId" placeholder="请选择课程">
          <el-option
            v-for="course in courseList"
            :key="course.id"
            :label="course.name"
            :value="course.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="附件">
        <div v-if="data.file" class="flex items-center justify-between w-full">
          <span>{{ data.file.name }}</span>
          <el-button type="text" @click="data.file = null">删除</el-button>
        </div>
        <el-button v-else type="primary" @click="emits('upload')"
          >上传附件</el-button
        >
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="emits('cancel')">取消</el-button>
        <el-button type="primary" @click="handleSave">发布</el-button>
      </span>
    </template>
  </el-dialog>
</template>
<script lang="ts" setup>
import type { FormInstance } from "element-plus";

const modelValue = defineModel<boolean>("modelValue");
const data = defineModel<any>("data");
defineProps<{
  courseList: any[];
}>();

const emits = defineEmits<{
  upload: [];
  save: [];
  cancel: [];
}>();

const assignmentFormRef = ref<FormInstance>();
function handleSave() {
  assignmentFormRef.value?.validate((valid) => {
    if (!valid) return;
    emits("save");
  });
}
</script>
<style scoped lang="scss"></style>
